using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Trippy.Camera;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;
using Trippy.Animation;
using Trippy.UI_Overlay;
using Color = Microsoft.Xna.Framework.Color;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace Trippy
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TrippyGame : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;

        public TrippyState currentState;

        public Vector2 spawnPoint;

        public Level savedLevel = null;

        //Keystates
        public Keys left = Keys.Left;
        public Keys right = Keys.Right;
        public Keys jump = Keys.Up;
        public Keys action = Keys.Space;
        public Keys menu = Keys.Back;

        public Song menuSong;
        public Song gameSong;
        public SoundEffect rainSound;
        public SoundEffectInstance rainLoop;
        public Song battleSong;

        public TrippyGame()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            Content.RootDirectory = "Content";
        }

        public List<DrawableTrippyComponent> AddQueue; //queue of objects that need to be added in a level. can only be handled here. =/

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //var firstLevel = new FirstLevel(this, null); // if no hero is passed as a parameter, one is automatically created
            
           //hero = firstLevel.Hero;

            //QUICKFIX HERO STUFF
            Hero.Lives = TrippyConstants.Agent.Hero.Lives;
            Hero.Score = 0;

            menuSong = Content.Load<Song>("Music/Rest");
            gameSong = Content.Load<Song>("Music/ForestSong");
            rainSound = Content.Load<SoundEffect>("Music/SFX/Rain2");
            battleSong = Content.Load<Song>("Music/BossMusic");
            rainLoop = rainSound.CreateInstance();
            rainLoop.IsLooped = true;
            var titleScreen = new TitleScreen(this);

            currentState = titleScreen;
            Components.Add(currentState);

            AddQueue = new List<DrawableTrippyComponent>();

            base.Initialize();
        }

        /// <summary>
        /// 
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            AnimatedSprite.spriteBatch = new SpriteBatch(GraphicsDevice);
            base.LoadContent();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (currentState is Level)
            {
                foreach (DrawableTrippyComponent x in AddQueue)
                {
                    (currentState as Level).Add(x);
                }
                AddQueue.Clear();
            }

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);

        }
    }
}
